/***************************************************************************
 *            option.c
 *
 *  Sa Januar 21 13:03:13 2012
 *  Copyright  2012  ich
 *  <user@host>
 ****************************************************************************/
/*
 * option.c
 *
 * Copyright (C) 2012 - ich
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


#ifdef __linux__
   #include <sys/types.h>
   #include <sys/stat.h>
   #include <unistd.h>
//   #define MODUS ,0711)
#elif _WIN32__ || _MS_DOS_
    #include <dir.h>
    #define MODUS )
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>

#include "option.h"


FILE *log_f;


int TFN_InitLog ()
{
	log_f = fopen ("log.txt", "w");
}

char* benutzercfg (void)
{
	char *bcfg, *gg;

	bcfg = (char*) malloc (sizeof(char)*255);

	gg = getenv ("HOME");

	strcpy (&bcfg[0], gg);

	if (bcfg == NULL)
	{
		fprintf (log_f, "\tKonnte Benutzerverzeichnis nicht ermitteln\n");
		return NULL;
	}

	strcat (bcfg, "/.tetrisfn/tetrisfn.cfg");

	return bcfg;
}

//TODO kann perdu keine benutzereinstellungen laden, aber warum???????
//     meinen test nach, ift fp zwar NULL, aber ich kann trotzdem aus der datei lesen, warum??????
int TFN_LadeCFG (TFN_Option_p opt)
{
	FILE *fp;
	char *bcfg;

	bcfg = benutzercfg ();
printf ("Benutzereinstellungen: %s\n", bcfg);
	fp = fopen (bcfg, "r");
	if (NULL == fp);
	{
		fprintf (log_f, "\tBenutzereinstellungen konnten nich gelaen werden!\n");
      		printf(" --> (%s)\n", strerror(errno));

		fp = fopen ("tetrisfn.cfg", "r");
		if (fp == NULL)
		{
			fprintf (log_f, "\tKonnte keine cfg-Datei laden\n");
			return -1;
		}
	}
	//jetz werden die werte geladen
	fscanf (fp, "Version: %s\n", &opt->ver[0]);
	fscanf (fp, "wbild_x: %d\n", &opt->wbild_x);
	fscanf (fp, "wbild_y: %d\n", &opt->wbild_y);
	fscanf (fp, "wbild_flags: %d\n", &opt->wbild_flags);
	fscanf (fp, "fsbild_x: %d\n", &opt->fsbild_x);
	fscanf (fp, "fsbild_y: %d\n", &opt->fsbild_y);
	fscanf (fp, "fsbild_bpp: %d\n", &opt->fsbild_bpp);
	fscanf (fp, "fsbild_flags: %d\n", &opt->fsbild_flags);
	fscanf (fp, "fps: %d\n", &opt->framerate);
	fscanf (fp, "theme: %s\n", &opt->theme[0]);
	fscanf (fp, "stein_runter_delay: %d\n", &opt->stein_runter_delay);
	fscanf (fp, "stein_linksrechts_delay: %d\n", &opt->stein_linksrechts_delay);
	fscanf (fp, "stein_drehen_delay: %d\n", &opt->stein_drehen_delay);
	fscanf (fp, "key_down: %c\n", &opt->brick_down);
	fscanf (fp, "key_right: %c\n", &opt->brick_right);
	fscanf (fp, "key_left: %c\n", &opt->brick_left);
	fscanf (fp, "key_rotat: %c\n", &opt->brick_rotat);
	fscanf (fp, "key_pause: %c\n", &opt->pause);
	fscanf (fp, "level_points: %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", &opt->lv2_p, &opt->lv3_p, &opt->lv4_p, &opt->lv5_p, &opt->lv6_p, &opt->lv7_p, &opt->lv8_p, &opt->lv9_p, &opt->lv10_p, &opt->lv11_p, &opt->lv12_p, &opt->lv13_p, &opt->lv14_p, &opt->lv15_p);
	fscanf (fp, "level_delay: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", &opt->lv1_d, &opt->lv2_d, &opt->lv3_d, &opt->lv4_d, &opt->lv5_d, &opt->lv6_d, &opt->lv7_d, &opt->lv8_d, &opt->lv9_d, &opt->lv10_d, &opt->lv11_d, &opt->lv12_d, &opt->lv13_d, &opt->lv14_d, &opt->lv15_d);

	fclose (fp);
	return 0;
		
}

int Benutzerverzeichnis_save (void)
{
	char *pathd,*gg;

	pathd = (char*) malloc (sizeof(char)*255);

	gg = getenv ("HOME");

	strcpy (pathd, gg);
	strcat (pathd, "/.tetrisfn");

		if(mkdir(pathd) == -1)
		{
      		printf("Konnte kein neues Benutzerverzeichnis erstellen\n");
      		printf(" --> (%s)\n", strerror(errno));
			return -1;
		}
	
	return 0;
}

int TFN_SpeicherCFG (TFN_Option_p opt)
{
	FILE *fp;
	char *paths;
	paths = benutzercfg ();
	Benutzerverzeichnis_save ();
	fp = fopen (paths, "w");

	fprintf (fp, "Version: %s\n", &opt->ver[0]);
	fprintf (fp, "wbild_x: %d\n", opt->wbild_x);
	fprintf (fp, "wbild_y: %d\n", opt->wbild_y);
	fprintf (fp, "wbild_flags: %d\n", opt->wbild_flags);
	fprintf (fp, "fsbild_x: %d\n", opt->fsbild_x);
	fprintf (fp, "fsbild_y: %d\n", opt->fsbild_y);
	fprintf (fp, "fsbild_bpp: %d\n", opt->fsbild_bpp);
	fprintf (fp, "fsbild_flags: %d\n", opt->fsbild_flags);
	fprintf (fp, "fps: %d\n", opt->framerate);
	fprintf (fp, "theme: %s\n", &opt->theme[0]);
	fprintf (fp, "stein_runter_delay: %d\n", opt->stein_runter_delay);
	fprintf (fp, "stein_linksrechts_delay: %d\n", opt->stein_linksrechts_delay);
	fprintf (fp, "stein_drehen_delay: %d\n", opt->stein_drehen_delay);
	fprintf (fp, "key_down: %c\n", opt->brick_down);
	fprintf (fp, "key_right: %c\n", opt->brick_right);
	fprintf (fp, "key_left: %c\n", opt->brick_left);
	fprintf (fp, "key_rotat: %c\n", opt->brick_rotat);
	fprintf (fp, "key_pause: %c\n", opt->pause);
	fprintf (fp, "level_points: %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", opt->lv2_p, opt->lv3_p, opt->lv4_p, opt->lv5_p, opt->lv6_p, opt->lv7_p, opt->lv8_p, opt->lv9_p, opt->lv10_p, opt->lv11_p, opt->lv12_p, opt->lv13_p, opt->lv14_p, opt->lv15_p);
	fprintf (fp, "level_delay: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", opt->lv1_d, opt->lv2_d, opt->lv3_d, opt->lv4_d, opt->lv5_d, opt->lv6_d, opt->lv7_d, opt->lv8_d, opt->lv9_d, opt->lv10_d, opt->lv11_d, opt->lv12_d, opt->lv13_d, opt->lv14_d, opt->lv15_d);

fclose (fp);

	return 0;
}
